Міністерство освіти і науки України
Національний університет „Львівська політехніка”
Кафедра електронних
обчислювальних машин
Звіт
про виконання лабораторної роботи № 4
з курсу „ Захист інформації в комп’ютерних системах ”
Хеш функції.Алгоритм хешування MD5.
Львів – 2005
Мета роботи: ознайомитися із хеш функціями та їх застосуванням, закріпити навики створення хеш функції на прикладі алгоритму хешування MD5.
Завдання: За допомогою алгоритму хешування MD5 отримати хеш вхідної стрічки.
Текст програми:
main.c
#define VERSION "2.0 (2003-04-15)"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif
#include "md5.h"
#define FALSE 0
#define TRUE 1
#define EOS '\0'
int main(argc, argv)
int argc; char *argv[];
{
int i, j, opt, cdata = FALSE, docheck = FALSE, showfile = TRUE, f = 0;
unsigned int bp;
char *cp, *clabel, *ifname, *hexfmt = "%02X";
FILE *in = stdin, *out = stdout;
unsigned char buffer[16384], signature[16], csig[16];
struct MD5Context md5c;
for (i = 1; i < argc; i++) {
cp = argv[i];
if (*cp == '-') {
if (strlen(cp) == 1) { i++; break; }
opt = *(++cp);
if (islower(opt)) { opt = toupper(opt); }
switch (opt) {
case 'C': /* -Csignature -- Check signature, set return code */
docheck = TRUE;
if (strlen(cp + 1) != 32) { docheck = FALSE; }
memset(csig, 0, 16);
clabel = cp + 1;
for (j = 0; j < 16; j++) {
if (isxdigit((int) clabel[0]) && isxdigit((int) clabel[1]) &&
sscanf((cp + 1 + (j * 2)), hexfmt, &bp) == 1) {
csig[j] = (unsigned char) bp;
} else { docheck = FALSE; break; }
clabel += 2;
}
if (!docheck) {
fprintf(stderr, "Error in signature specification. Must be 32 hex digits.\n");
return 2;
}
break;
case 'D': /* -Dtext -- Compute signature of given text */
MD5Init(&md5c); MD5Update(&md5c, (unsigned char *) (cp + 1), strlen(cp + 1));
cdata = TRUE; f++; break; /* Mark no infile argument needed */
case 'L': /* -L -- Use lower case letters as hex digits */
hexfmt = "%02x"; break;
case 'N': /* -N -- Don't show file name after sum */
showfile = FALSE; break;
case 'O': /* -Ofname -- Write output to fname (- = stdout) */
cp++;
if (strcmp(cp, "-") != 0) {
if (out != stdout) {
fprintf(stderr, "Redundant output file specification.\n");
return 2;
}
if ((out = fopen(cp, "w")) == NULL) {
fprintf(stderr, "Cannot open output file %s\n", cp); return 2;
}
}
break;
case '?': /* -U, -? -H -- Print how to call information. */
case 'H':
case 'U':
printf("\nMD5 -- Calculate MD5 signature of file. Call");
printf("\n with md5 [ options ] [file ...]\n");
printf("\n Options:");
printf("\n -csig Check against sig, set exit status 0 = OK");
printf("\n -dtext Compute signature of text argument");
printf("\n -l Use lower case letters for hexadecimal digits");
printf("\n -n Do not show file name after sum");
printf("\n -ofname Write output to fname (- = stdout)");
printf("\n -u Print this message");
printf("\n -v Print version information\n");
printf("\nby John Walker -- http://www.fourmilab.ch/");
printf("\nVersion %s\n", VERSION);
printf("\nThis program is in the public domain.\n\n");
return 0;
case 'V': /* -V -- Print version number */
printf("%s\n", VERSION);
return 0;
}
} else { break; }
}
if (cdata && (i < argc)) {
fprintf(stderr, "C...